n, m = [int(i) for i in input().split()]
m = []
for i in range(n):
m.append(input())
for i in range(n):
if m[i][0] == '1' or m[i][-1] == '1':
if i == 0 or i == n-1:
print(1)
else:
print(2)
exit()
if '1' in m[i] and (i == 0 or i == n-1):
print(2)
exit()
print(4)
// sai_mun
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pis pair<int, string>
#define Pi pair<int, int>
#define Pl pair<ll>
#define rep0(i, n) for (__typeof(n) i = 0; i < (n); i++)
#define rep1(i, n) for (__typeof(n) i = 1; i <= (n); i++)
#define push_back pb
const int INF = 1e9;
const ll INFll = 1e18;
const int MAX = 1e5 + 5;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int main()
{
int n,m,a,ans=0; cin>>n>>m;
rep1(i,n){
rep1(j,m){
cin>>a;
if(a){
if(i==1 || j==1 || i==n || j==m)ans=2;
}
}
}
if(!ans)cout<<4<<endl;
else cout<<ans<<endl;
}
844B - Rectangles | 1591A - Life of a Flower |
1398C - Good Subarrays | 629A - Far Relative’s Birthday Cake |
1166A - Silent Classroom | 1000B - Light It Up |
218B - Airport | 1463B - Find The Array |
1538C - Number of Pairs | 621B - Wet Shark and Bishops |
476B - Dreamoon and WiFi | 152C - Pocket Book |
1681D - Required Length | 1725D - Deducing Sortability |
1501A - Alexey and Train | 721B - Passwords |
1263D - Secret Passwords | 1371B - Magical Calendar |
1726E - Almost Perfect | 1360C - Similar Pairs |
900A - Find Extra One | 1093D - Beautiful Graph |
748A - Santa Claus and a Place in a Class | 1511B - GCD Length |
676B - Pyramid of Glasses | 597A - Divisibility |
1632A - ABC | 1619D - New Year's Problem |
242B - Big Segment | 938A - Word Correction |